/**
 * Definition for singly-linked list.
 * 
 * 
 * 
 * 
 */

 function ListNode(val, next) {
  this.val = (val===undefined ? 0 : val)
  this.next = (next===undefined ? null : next)
}

let Node1 = new ListNode(1)
let Node2 = new ListNode(2)
let Node3 = new ListNode(3)
let Node4 = new ListNode(4)
let Node5 = new ListNode(5)
Node1.next = Node2
Node2.next = Node3
Node3.next = Node4
Node4.next = Node5

/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var swapPairs = function (head) {
  let h = (p = new ListNode())
  p.next = head

  let cur
  let next
  while (p.next&&p.next.next) {
    let tmp = p
    cur = p.next
    next = cur.next
    
    cur.next = next.next
    next.next = cur
    p = p.next
    tmp.next = next
  }

  return h.next
}

console.log(swapPairs(Node1));
